//
//  T3FMapPlatformView+Polyline.swift
//  t3_map
//
//  Created by Cunhai Li on 2024/10/31.
//

import Foundation
import Bundle_Log

/// polyline处理
extension T3FMapPlatformView {

    /// 添加线段
    func add(polyline arguments: Any?) {

        if let jsonString = arguments as? String {

            if let polylineOptions = T3FPolylineOptions(from: jsonString) {
                do {
                    let _ = try mapView.add(polyline: polylineOptions)
                } catch (let error) {
                    T3Log.error("地图线段添加异常：error = \(error.localizedDescription)")
                }
            }
            else {
                T3Log.error("地图线段添加异常：arguments解析失败")
            }
        }
        else {
            T3Log.error("地图线段添加异常：arguments类型转换失败，目标类型为String")
        }
    }

    /// 更新线段
    func update(polyline arguments: Any?) {
        if let jsonString = arguments as? String {

            if let polylineOptions = T3FPolylineOptions(from: jsonString) {
                mapView.update(polyline: polylineOptions)
            }
            else {
                T3Log.error("地图线段更新异常：arguments解析失败")
            }
        }
        else {
            T3Log.error("地图线段更新异常：arguments类型转换失败，目标类型为String")
        }
    }
    
    func exchange(polylines arguments: Any?) {
        if let jsonString = arguments as? String, let jsonData = jsonString.data(using: .utf8) {
            do {
                if let jsonArray = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String] {
                    mapView.exchange(polylines: jsonArray)
                }
            } catch {
                print("JSON parsing error: \(error)")
            }
        }
    }

    /// 移除线段
    func remove(polyline arguments: Any?) {

        if let jsonString = arguments as? String {

            if let polylineOptions = T3FPolylineOptions(from: jsonString) {

                let _ = mapView.remove(polyline: polylineOptions)
            }
            else {
                T3Log.error("地图线段移除异常：arguments解析失败")
            }
        }
        else {
            T3Log.error("地图线段移除异常：类型转换失败，目标类型为String")
        }
    }

    /// 在可视区域内显示Polyline
    func show(polylines arguments: Any?) {
        do {
            let json = try T3FJsonUtil.toJson(arguments)
            guard let polylines = json["polylines"] as? [[String: Any]] else { return }

            var polylineOptionsList: [T3FPolylineOptions] = []
            for polyline in polylines {
                if let polylineOptions = T3FPolylineOptions(from: polyline) {
                    polylineOptionsList.append(polylineOptions)
                }
                else {
                    T3Log.error("polylines初始化异常，参数错误，continue")
                }
            }

            let animated = json["animated"] as? Bool ?? true

            var edgeInsets = UIEdgeInsets.zero
            if let edgeInsetsValue = json["edgeInsets"] as? [String: Double] {
                edgeInsets = T3FJsonUtil.edgeInsetsFrom(json: edgeInsetsValue) ?? .zero
            }

            mapView.show(polylines: polylineOptionsList, edgePadding: edgeInsets, animated: animated)
        } catch (let error) {
            T3Log.error("polylines初始化异常，error = \(error.localizedDescription)")
        }
    }

    func toPolylineOptions(from arguments: Any?) -> [T3FPolylineOptions]? {
        do {
            let json = try T3FJsonUtil.toJson(arguments)
            if let polylines = json["polylines"] as? [[String: Any]] {
                var polylineOptionsList: [T3FPolylineOptions] = []
                for polyline in polylines {
                    if let polylineOptions = T3FPolylineOptions(from: polyline) {
                        polylineOptionsList.append(polylineOptions)
                    }
                    else {
                        T3Log.error("polylines初始化异常，参数错误，continue")
                    }
                }
                return polylineOptionsList
            }
            else {
                T3Log.error("polylines初始化异常，未找到`polylines`字段或者polylines字段类型转换异常")
                return nil
            }
        } catch (let error) {
            T3Log.error("polylines初始化异常，error = \(error.localizedDescription)")
            return nil
        }
    }
}
